Skip to content

Cache Kubernetes clientset outside node condition ticker loop#127

Draft
Copilot wants to merge 2 commits intorunzhen/npd2from
copilot/sub-pr-126
Draft

Cache Kubernetes clientset outside node condition ticker loop#127
Copilot wants to merge 2 commits intorunzhen/npd2from
copilot/sub-pr-126

Conversation

Copy link
Contributor

Copilot AI commented Mar 12, 2026

clientcmd.BuildConfigFromFlags and kubernetes.NewForConfig were called on every tick of the 1-minute loop, incurring unnecessary file I/O and allocations each cycle.

Changes

  • Clientset lifecycle: A buildClientset closure builds the REST config and clientset once before the loop. Inside the loop it is only invoked when clientset == nil.
  • Failure-triggered recreation: After a failed Nodes().Get() call, clientset is set to nil so the next tick transparently rebuilds it — covering kubeconfig rotation and transient API server unavailability.
  • Goroutine resilience: Previous return statements on per-tick errors replaced with continue; transient failures no longer kill the goroutine.
  • Initial failure visibility: If the pre-loop build fails, an info log is emitted to make the first-tick retry observable.
// Built once; reused every tick
buildClientset()

for {
    select {
    case <-ticker.C:
        if clientset == nil && !buildClientset() {
            continue
        }
        node, err := clientset.CoreV1().Nodes().Get(...)
        if err != nil {
            clientset = nil  // force rebuild next tick
            continue
        }
        ...
    }
}

📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

…lure

Co-authored-by: runzhen <32292691+runzhen@users.noreply.github.com>
Copilot AI changed the title [WIP] [WIP] Address feedback on node condition remediation PR Cache Kubernetes clientset outside node condition ticker loop Mar 12, 2026
Copilot AI requested a review from runzhen March 12, 2026 21:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants